/*qw6QefI/9grLT0CViR6DSIhqBYDVXpKVL86x8Hgeb+w=*/
import { viewModel } from 'nc-lightapp-front';
export default function updateTableDataTs(param){
    let {props,data,tableId}=param;
    let resultData={},pkTots={};
    let successData=data.grid[tableId]?data.grid[tableId]:data.grid;
    let checked_allpks=successData.allpks;
    let checked_alltss=successData.alltss;
    let resRows=successData.rows;
    resRows&&resRows.forEach((resRow,index)=>{
        if(resRow.rowid){
            resultData[resRow.rowid]=resRow;
        }
    })
    checked_allpks.forEach((pk,index)=>{
        pkTots[pk]=checked_alltss[index];
    })
    let allTableRows=props.table.getAllTableData(tableId);
    let allTableRows_row=allTableRows.rows;
    let allTableRows_allpks=allTableRows.allpks;
    let allTableRows_alltss=allTableRows.alltss;
    //更新ts
    allTableRows_allpks.forEach((pk,index)=>{
        if(pkTots[pk]){
            allTableRows_alltss[index]=pkTots[pk];
        }
    })
    //更新行数据rows
    let updateRows=allTableRows_row.map((row,index)=>{
        if(resultData[row.rowid]){
            row.values=resultData[row.rowid].values;
        }
        return row;
    })
    allTableRows.rows=updateRows;
    props.table.setAllTableData(tableId, allTableRows);
}
/**
 * 列表删除数据根据rowid来更新当前页面数据
 * PopupWindowStyle:'success'
 * errMsg:''
 * successIndexs:[]
 * successPKs:[]
 * **/
 export function deleteTableRowsByRowIds(param){
    let {props,data,rowIds,tableId,pkname}=param;
    let deleteRowIds=[];
    let successPKs=data.successPKs;
    if(successPKs&&successPKs.length>0){
        successPKs.forEach((pk,index)=>{
            if(rowIds[pk]){
                deleteRowIds.push(rowIds[pk]);
            }
        })
    }
    if(deleteRowIds&&deleteRowIds.length>0){
        props.table.deleteTableRowsByRowId(tableId, deleteRowIds, { deleteCachePk: true });
    }
}
//------------------修改处----------------------
/**
 * 列表删除数据根据rowid来更新当前页面数据
 * PopupWindowStyle:'success'
 * errMsg:''
 * successIndexs:[]
 * successPKs:[]
 * **/
export function deleteTableRowsByRowIds(param){
    let {props,data,rowIds,tableId,pkname}=param;
    let deleteRowIds=[];
    let successPKs=data.successPKs;
    if(successPKs&&successPKs.length>0){
        successPKs.forEach((pk,index)=>{
            if(rowIds[pk]){
                deleteRowIds.push(rowIds[pk]);
            }
        })
    }
    if(deleteRowIds&&deleteRowIds.length>0){
        props.table.deleteTableRowsByRowId(tableId, deleteRowIds, { deleteCachePk: true });
    }
}

/**
 * 列表删除数据需要同时删除缓存里面的pk和ts
 * props:props
 * checkedRows:[]
 * successPKs:[]//删除成功的pk
 * successTSs:[]//删除成功的pk对应的ts
 * tableId:tableId
 * */
 export function deleteCachePKAndTS(param){
    let {props,checkedRows,successPKs,tableId,pkId,pkname}=param;
    if(!pkId){
        pkId='pk_bill';
    }
    let successTSs=[];
    checkedRows.forEach((item,index)=>{
        if(successPKs.includes(item[pkId])){
            if(item.ts){
                successTSs.push(item.ts);
            }
        }
    })
    props.table.deleteCacheId(tableId,successPKs,successTSs);
}
export function getAllCheckedDataNew (props, tableId, billType,pkname) {
	let checkedData = props.table.getCheckedRows(tableId);
	let delObjs = [],rowIds={};
	checkedData.forEach((val) => {
		delObjs.push(Object.assign({
			pk_bill: val.data.values?val.data.values[pkname].value:val.data.pk,
			billno: val.data.values?val.data.values.billno.value:val.data.billno||'',
			ts: val.data.values?val.data.values.ts.value:val.data.ts,
			billType: billType,
			index: val.index,
			rowid:val.data&&val.data.rowid?val.data.rowid:'',
			pageId: props.getSearchParam('p')
		}, this.dataInSaga));
		if(val.data && val.data.rowId){
			let pk=val.data.values[pkname].value;
			rowIds[pk]=val.data.rowId;
		}
	});
	return {delObjs,rowIds};
};

export function updateTableRecordTs(param){
    let {props,data,tableId,pkname}=param;
    let pkTots={};
    let successRecord=data[tableId].rows[0].values;
    let currentRecordPk=successRecord[pkname].value;
    let currentRecordTs=successRecord['ts'].value;
    if(currentRecordPk){
        pkTots[currentRecordPk]=currentRecordTs;
    }
    let allTableRows=props.table.getAllTableData(tableId);
    let allTableRows_row=allTableRows.rows;
    let allTableRows_allpks=allTableRows.allpks;
    let allTableRows_alltss=allTableRows.alltss;
    //更新ts
    allTableRows_allpks.forEach((pk,index)=>{
        if(pkTots[pk]){
            allTableRows_alltss[index]=pkTots[pk];
        }
    })
    //更新行数据rows
    let updateRows=allTableRows_row.map((row,index)=>{
        if(row.values[pkname].value==currentRecordPk){
            row.values=successRecord;
        }
        return row;
    })
    allTableRows.rows=updateRows;
    props.table.setAllTableData(tableId, allTableRows);
}
export function updateCacheTss(param){
    let {dataSource,pkname,data,tableId,formId}=param;
    let dataSource_Data = viewModel.getData(dataSource);
    let resPk=data.head[formId]&&data.head[formId].rows[0]&&data.head[formId].rows[0].values[pkname].value;
    let resTs=data.head[formId]&&data.head[formId].rows[0]&&data.head[formId].rows[0].values['ts'].value;
    let allpks=dataSource_Data.simpleTable.allpks;
    let alltss=dataSource_Data.simpleTable.alltss;
    if(alltss&&allpks){
        for(let index=0;index<allpks.length;index++){
            if(resPk==allpks[index]){
                alltss[index]=resTs;
                break;
            }
        }
        viewModel.setData(dataSource, dataSource_Data);
    }
}
/*qw6QefI/9grLT0CViR6DSIhqBYDVXpKVL86x8Hgeb+w=*/